﻿#include "scutcheonhandler.h"
#include "screenscutcheon.h"
#include "graphic/borderpaneloverlayelement.h"
#include "scutcheonevent.h"
#include "scutcheoneventobject.h"

using namespace EarthView::World::Spatial::SystemUI;
using namespace EarthView::World::Spatial3D::Controls;
using namespace EarthView::World::Spatial::Math;
using namespace EarthView::World::Core;
using namespace EarthView::World::Graphic;

CScutcheonHandler* CScutcheonHandler::mpSingleton = NULL;

CScutcheonHandler::CScutcheonHandler()
{
	this->ref_mpGlobeControl=NULL;
	mpEventObject = NULL;
	mScutcheonName= "";
}

CScutcheonHandler::~CScutcheonHandler()
{
	mScutcheonVector.clear();
	if(ref_mpGlobeControl)
		ref_mpGlobeControl->removeHandler(this);
	this->ref_mpGlobeControl = NULL;
}

CScutcheonHandler* CScutcheonHandler::getSingletonPtr()
{
	if (!mpSingleton)
	{
		mpSingleton=EV_NEW CScutcheonHandler();
	}
	return mpSingleton;
}

ev_bool CScutcheonHandler::handleEvent(CGUIEvent* guiEvent)
{
	if(guiEvent->getEventType() == CGUIEvent::MOUSEMOVE)
	{
		return handleMouseMoveEvent(guiEvent);
	}
	return false;
}

ev_bool CScutcheonHandler::handleMouseMoveEvent(CGUIEvent* guiEvent)
{
	if (mScutcheonVector.size() < 1) return false;
	ev_int32  x = guiEvent->getX();
	ev_int32  y = guiEvent->getY();
	computeSelected(x,y);
	return false;
}

void CScutcheonHandler::setGlobeControl(CGlobeControl* ref_globeControl)
{
	this->ref_mpGlobeControl=ref_globeControl;
	this->ref_mpGlobeControl->addGuiHandler(this);
}

void CScutcheonHandler::setEventObject(CScutcheonEventObject* ref_eventObject)
{
	mpEventObject = ref_eventObject;
}

void CScutcheonHandler::computeSelected(ev_int32 x, ev_int32 y)
{
	if (!mpEventObject) return;
	vector<CScreenScutcheon*>::iterator itr = mScutcheonVector.begin();
	for (; itr != mScutcheonVector.end(); itr++)
	{
		CScreenScutcheon*  scutcheon = *itr;
		if(scutcheon->getVisible())
		{
			bool m=isInBoundingBox(scutcheon,x,y);
			bool b = (mScutcheonName == scutcheon->getName());
			if (!b)
			{
				if (m)
				{
					mScutcheonName= scutcheon->getName();
					CScutcheonEvent evt(CScutcheonEvent::SET_MouseOn);	
					evt.setSelectedScutcheon(scutcheon);
					if (scutcheon->getEventObject())
					{
						CEventDispatcher::sendEvent(mpEventObject, &evt);
					}
				}
				/*else
				{
				CScutcheonEvent evt(CScutcheonEvent::SET_MouseOn);	
				evt.setSelectedScutcheon(NULL);
				if (scutcheon->getEventObject())
				{
				CEventDispatcher::sendEvent(mpEventObject, &evt);
				}
				}*/
			}
		
		}
	}
}

bool CScutcheonHandler::isInBoundingBox(CScreenScutcheon* ref_scutcheon, const int& x, const int& y)
{
	CVector2 pos = ref_scutcheon->getPanelElementPostion(ref_scutcheon->getName());
	bool m1=  x>=pos.x && x<=pos.x+ref_scutcheon->getPanelElementWidth(ref_scutcheon->getName());
	bool m2= y>=pos.y && y<=pos.y+ref_scutcheon->getPanelElementHeight(ref_scutcheon->getName());
	bool m=m1 && m2;
	if (m)
	{
		return true;
	}
	return false;
}

void CScutcheonHandler::addScutcheon(CScreenScutcheon* ref_scutcheon)
{
      mScutcheonVector.push_back(ref_scutcheon);
}